/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ncrnadb.ncinetview.internal.ui;


import java.util.Collection;
import java.util.Set;
import javax.swing.JComponent;
import javax.swing.JOptionPane;

import ncrnadb.ncinetview.internal.NcINetViewApp;
import ncrnadb.ncinetview.internal.request.ExpandRequest;

import org.cytoscape.model.CyColumn;
import org.cytoscape.model.CyNetwork;

/**
 *
 * @author vbonnici
 */
public class QueryFromColumnPanel extends javax.swing.JPanel {

    
    private QueryDialog parentToDispose = null;
    
    /**
     * Creates new form QueryFromColumnPanel
     */
    public QueryFromColumnPanel(QueryDialog parentToDispose) {
        initComponents();
        
        this.parentToDispose = parentToDispose;
                
        refreshNetworkList();
        refreshColumnList();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        AliasColumnListjComboBox = new javax.swing.JComboBox();
        NetworkListjComboBox = new javax.swing.JComboBox();
        TypeColumnListjComboBox = new javax.swing.JComboBox();
        neighsNcrnajCheckBox = new javax.swing.JCheckBox();
        jLabel2 = new javax.swing.JLabel();
        typeGene = new javax.swing.JCheckBox();
        typeOthers = new javax.swing.JCheckBox();
        jSeparator4 = new javax.swing.JSeparator();
        neighsOthersjCheckBox = new javax.swing.JCheckBox();
        jSeparator3 = new javax.swing.JSeparator();
        typeRNA = new javax.swing.JCheckBox();
        includeNeighsCheckBox = new javax.swing.JCheckBox();
        typeDisease = new javax.swing.JCheckBox();
        jLabel3 = new javax.swing.JLabel();
        typeNcrna = new javax.swing.JCheckBox();
        selectedOnly = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();
        neighsDiseasejCheckBox = new javax.swing.JCheckBox();
        neighsGenejCheckBox = new javax.swing.JCheckBox();
        neighsRnajCheckBox = new javax.swing.JCheckBox();

        jLabel1.setText("Network");

        NetworkListjComboBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                NetworkListjComboBoxItemStateChanged(evt);
            }
        });
        NetworkListjComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NetworkListjComboBoxActionPerformed(evt);
            }
        });

        neighsNcrnajCheckBox.setText("ncRNA");
        neighsNcrnajCheckBox.setEnabled(false);

        jLabel2.setText("Type column");

        typeGene.setSelected(true);
        typeGene.setText("Gene");

        typeOthers.setSelected(true);
        typeOthers.setText("Others");

        neighsOthersjCheckBox.setText("Others");
        neighsOthersjCheckBox.setEnabled(false);

        typeRNA.setSelected(true);
        typeRNA.setText("RNA");

        includeNeighsCheckBox.setText("Include neighbours");
        includeNeighsCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                includeNeighsCheckBoxStateChanged(evt);
            }
        });

        typeDisease.setSelected(true);
        typeDisease.setText("Disease");

        jLabel3.setText("Alias column");

        typeNcrna.setSelected(true);
        typeNcrna.setText("ncRNA");

        selectedOnly.setText("selected only");
        selectedOnly.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectedOnlyActionPerformed(evt);
            }
        });

        jButton1.setText("Import");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        neighsDiseasejCheckBox.setText("Disease");
        neighsDiseasejCheckBox.setEnabled(false);

        neighsGenejCheckBox.setText("Gene");
        neighsGenejCheckBox.setEnabled(false);

        neighsRnajCheckBox.setText("RNA");
        neighsRnajCheckBox.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(neighsNcrnajCheckBox)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(neighsRnajCheckBox)
                        .addGap(12, 12, 12)
                        .addComponent(neighsGenejCheckBox)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(neighsDiseasejCheckBox)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(neighsOthersjCheckBox)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(NetworkListjComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(TypeColumnListjComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jSeparator3)
                            .addComponent(jSeparator4)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(108, 108, 108)
                                        .addComponent(selectedOnly))
                                    .addComponent(includeNeighsCheckBox)
                                    .addComponent(jButton1))
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(22, 22, 22)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(typeNcrna)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(typeRNA)
                                        .addGap(12, 12, 12)
                                        .addComponent(typeGene)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(typeDisease)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(typeOthers)
                                        .addGap(0, 107, Short.MAX_VALUE))
                                    .addComponent(AliasColumnListjComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(NetworkListjComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(selectedOnly)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TypeColumnListjComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(AliasColumnListjComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(typeNcrna)
                    .addComponent(typeRNA)
                    .addComponent(typeGene)
                    .addComponent(typeDisease)
                    .addComponent(typeOthers))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(includeNeighsCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(neighsNcrnajCheckBox)
                    .addComponent(neighsRnajCheckBox)
                    .addComponent(neighsGenejCheckBox)
                    .addComponent(neighsDiseasejCheckBox)
                    .addComponent(neighsOthersjCheckBox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void NetworkListjComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_NetworkListjComboBoxItemStateChanged
        refreshColumnList();
    }//GEN-LAST:event_NetworkListjComboBoxItemStateChanged

    private void NetworkListjComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NetworkListjComboBoxActionPerformed
        refreshColumnList();
    }//GEN-LAST:event_NetworkListjComboBoxActionPerformed

    private void includeNeighsCheckBoxStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_includeNeighsCheckBoxStateChanged
        boolean en = this.includeNeighsCheckBox.isSelected();
        
            this.neighsDiseasejCheckBox.setEnabled(en);
            this.neighsGenejCheckBox.setEnabled(en);
            this.neighsNcrnajCheckBox.setEnabled(en);
            this.neighsRnajCheckBox.setEnabled(en);
            this.neighsOthersjCheckBox.setEnabled(en);
        
    }//GEN-LAST:event_includeNeighsCheckBoxStateChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    	
    	try{
    		System.out.println("request...");
        ExpandRequest request = new ExpandRequest();
        
        request.network = (CyNetwork)this.NetworkListjComboBox.getSelectedItem();
        
        request.sourceTypeCol = ((CyColumnItem)this.TypeColumnListjComboBox.getSelectedItem()).column;
        request.sourceTypeNcrna = this.typeNcrna.isSelected();
        request.sourceTypeRna = this.typeRNA.isSelected();
        request.sourceTypeGene = this.typeGene.isSelected();
        request.sourceTypeOthers = this.typeOthers.isSelected();
        request.sourceTypeDisease = this.typeDisease.isSelected();
        
        request.sourceNameCol = ((CyColumnItem)this.AliasColumnListjComboBox.getSelectedItem()).column;
        request.onlySelected = this.selectedOnly.isSelected();
        
        request.expand = this.includeNeighsCheckBox.isSelected();
        request.expandWithDisease = this.neighsDiseasejCheckBox.isSelected();
        request.expandWithGene = this.neighsGenejCheckBox.isSelected();
        request.expandWithNcrna = this.neighsNcrnajCheckBox.isSelected();
        request.expandWithRna = this.neighsRnajCheckBox.isSelected();
        request.expandWithOthers = this.neighsOthersjCheckBox.isSelected();
        
        
        request.solveRequest();
        
        parentToDispose.dispose();
    	}catch(Exception e){
    		JOptionPane.showMessageDialog(this, e.getMessage()+"\n"+e.getCause()+"\n"+e.getLocalizedMessage()+"\n"+e);
    		e.printStackTrace();
    	}
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void selectedOnlyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectedOnlyActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_selectedOnlyActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox AliasColumnListjComboBox;
    private javax.swing.JComboBox NetworkListjComboBox;
    private javax.swing.JComboBox TypeColumnListjComboBox;
    private javax.swing.JCheckBox includeNeighsCheckBox;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JCheckBox neighsDiseasejCheckBox;
    private javax.swing.JCheckBox neighsGenejCheckBox;
    private javax.swing.JCheckBox neighsNcrnajCheckBox;
    private javax.swing.JCheckBox neighsOthersjCheckBox;
    private javax.swing.JCheckBox neighsRnajCheckBox;
    private javax.swing.JCheckBox selectedOnly;
    private javax.swing.JCheckBox typeDisease;
    private javax.swing.JCheckBox typeGene;
    private javax.swing.JCheckBox typeNcrna;
    private javax.swing.JCheckBox typeOthers;
    private javax.swing.JCheckBox typeRNA;
    // End of variables declaration//GEN-END:variables

    
    private void refreshNetworkList(){
        this.NetworkListjComboBox.removeAllItems();
        
        Set<CyNetwork> scn = NcINetViewApp.app().cnm.getNetworkSet();
        for(CyNetwork n : scn){
            //System.out.println("# "+n);
            this.NetworkListjComboBox.addItem(n);
        }
    }
    
    
    private void refreshColumnList(){
        this.TypeColumnListjComboBox.removeAllItems();
        this.AliasColumnListjComboBox.removeAllItems();
        
        
        this.TypeColumnListjComboBox.addItem(new CyColumnItem(null));
        this.AliasColumnListjComboBox.addItem(new CyColumnItem(null));
        
        CyNetwork net = (CyNetwork)this.NetworkListjComboBox.getSelectedItem();
        if(net != null){
            Collection<CyColumn> cols = net.getDefaultNodeTable().getColumns();
            for(CyColumn c : cols){
                    //System.out.println("# "+c);
                    this.TypeColumnListjComboBox.addItem(new CyColumnItem(c));
                    this.AliasColumnListjComboBox.addItem(new CyColumnItem(c));
               }
        }
    }


}
